<template>
    <div class='readCard'>
        <el-button type="primary" @click="btnClicks">读取</el-button>
        <iframe :src="src" style="height:0px;position:absolute;top:-1050px" id="readCards" name="readCards"></iframe>
    </div>
   
</template>
<script>
import HighShot  from './HighShot'
export default {
    props:['id'],
    name:'read-card',
    components: {
        HighShot
    },
    data(){
        return{
            src:'',
            baseUrl:"ws://127.0.0.1:12345",
            socket:null,
            first:true,
            isRecieve:false,
        }
    },
    created(){
         
       /* this.src=localStorage.getItem("ip")+"apiServe/highShot";
        let sef=this;
        try{
            window.addEventListener('message', function(messageEvent) {
                var data = messageEvent.data;
                if(data&&typeof(data)=='string'&&data.indexOf("{")!=-1){
                    let item=JSON.parse(data);
                    let ids=sef.id.split("|");
                    let age="";
                    for(let i=0;i<ids.length;i++){
                        let id_=ids[i].split(":");
                        let datas={};
                        let val=item[id_[0]];
                        if(id_[0]=="mz"){
                            val=val+"族";
                        }
                        if(id_[0]=="Born"&&val){
                            let year=val.substring(0,4);
                            let month=val.length>6 ? val.substring(4,6):"";
                            let day=val.length==8 ? val.substring(6):"";
                            val=year+"-"+month+"-"+day;
                                                
                            var date = new Date();
                            let curYear=date .getFullYear(); //获取完整的年份(4位)
                            age=Number(curYear)-Number(year);
                            age=age.toFixed(0);

                        }
                        if(id_[0]=="age")
                            val=age;
                        datas.id=id_[1];
                        datas.val=val;

                        sef.$emit("input",JSON.stringify(datas));
                    }
                }
                
            },false);
        }catch(e){}*/
    },
     beforeDestroy(){
       
        let sef=this;
        if(sef.socket)
            sef.socket.close();
        this.GLOBAL.dialogs=null;
        this.GLOBAL.readId="";
    },
    methods:{
        btnClicks(){
            
            /*if(this.first){
                let sef=this;
                window.setTimeout(function(){
                     sef.openSocket(); 
                },1500)
               
                this.first=false;
            }*/
            this.GLOBAL.readId=this.id;
            if(this.GLOBAL.dialogs==null)
                this.openSocket();
            else{
                this.$message.error('上一次读取还未结束,请不要重复读取');
            }
           // else
            //    this.readCard(); 
        },
        readCard(){
            let isReadInfo=false;
            let isReadImg=false;
            let dialog=this.GLOBAL.dialogs;
            let sef=this;
             //反初始化
            /* document.getElementById("closeHtml").onclick = function() {
                dialog.get_actionType("closeSignal");
                var element = document.getElementById("bigPriDev");
                element.src = "";
                var element1 = document.getElementById("bigSubDev");
                element1.src = "";							
            };
            //初始化
            document.getElementById("openHtml").onclick = function() {
                dialog.html_loaded("two");			
            };*/
            
            //单次二代证阅读
            dialog.get_actionType("singleReadIDCard");	
            
            
            
            //服务器返回消息
            dialog.sendPrintInfo.connect(function(message) {
                if(message.indexOf("IDcardInfo:")!=-1){
                    isReadInfo=true;
                   
                    message=message.replace("IDcardInfo:","");
                    let msgs=message.split(" ");
                    let datas={};
                    datas.labelA=msgs[0];
                    datas.Sex=msgs[1];
                    datas.mz=msgs[2]+"族";
                    datas.Born=msgs[3]+"-"+msgs[4]+"-"+msgs[5];
                    datas.Address=msgs[6];
                    datas.CardNo=msgs[7];
                    var date = new Date();
                    let curYear=date .getFullYear(); //获取完整的年份(4位)
                    let age=Number(curYear)-Number(msgs[3]);
                    age=age.toFixed(0);
                    datas.age=age;
                    
                    if(sef.GLOBAL.readId&&sef.id!=sef.GLOBAL.readId){
                        return;
                    }
                    //let ids=sef.GLOBAL.readId.split("|");
                    let ids=sef.id.split("|");
                    for(let i=0;i<ids.length;i++){
                        let id_=ids[i].split(":");
                        if(id_[0]!="image"){
                            let data={};
                            let val=datas[id_[0]];
                            data.id=id_[1];
                            data.val=val;
                            sef.$emit("input",JSON.stringify(data));
                        }
                        
                    }

                }
                if(message.indexOf("idFaceInfo")!=-1){
                    isReadImg=true;
                    //message="data:image/jpeg;base64,"+message.replace("idFaceInfo:","");
                    message=message.replace("idFaceInfo:","");
                    //let ids=sef.GLOBAL.readId.split("|");
                    if(sef.GLOBAL.readId&&sef.id!=sef.GLOBAL.readId){
                        return;
                    } 
                    let ids=sef.id.split("|");
                    for(let i=0;i<ids.length;i++){
                        let id_=ids[i].split(":");
                        if(id_[0]=="image"){
                            sef.$post("business","/api/file/fileUploadBase64",{data:message,model:"头像",type:"头像",name:"头像"}).then(data=>{
                               
                                let url=data[0].url;
                                console.log("uploadImg==========================");
                                console.log(url);
                                let data_={};
                                data_.id=id_[1];
                                data_.val=url;
                                sef.$emit("input",JSON.stringify(data_));
                            });	
                            /*let data={};
                            data.id=id_[1];
                            data.val=message;
                            sef.$emit("input",JSON.stringify(data));*/
                        }
                        
                    }
                   // sef.socket.close();
                } 
                if(isReadInfo&&isReadImg){
                     dialog.get_actionType("stopIDCard");	
                     sef.closeStock();
                }
                    
                //dialog.get_actionType("closeSignal");
                
            });
            window.setTimeout(function(){
                dialog.get_actionType("stopIDCard");	
                sef.closeStock();
            },2000)
        },
        closeStock(){
             let sef=this;
            if(sef.socket)
                sef.socket.close();

            this.GLOBAL.dialogs=null;
            this.GLOBAL.readId="";
        },
        openSocket(){
            
            this.socket = new WebSocket(this.baseUrl);
            this.socket.onclose = function()
            {
                console.error("web channel closed");
            };
            this.socket.onerror = function(error)
            {
                console.error("web channel error: " + error);
            };
            let sef=this;
            this.socket.onopen = function()
            {
               
               
                sef.GLOBAL.sockets=sef.socket;
                 sef.$qweb(sef.socket, function(channel) {
                    //获取注册的对象
                    window.dialog = channel.objects.dialog;
                    //网页关闭函数
                    window.onbeforeunload = function() {
                        dialog.get_actionType("closeSignal");
                    }
                    window.onunload = function() {
                        dialog.get_actionType("closeSignal");
                    }
                    sef.GLOBAL.dialogs=dialog;
                    sef.readCard();
                });
            }
        }
    }
}
</script>
<style scoped>
   .readCard{
       display:inline-block;
       text-align: center;
       margin-left:10px;
   }
</style>